const WeChatPayModel = require('../model').weChatPayModel;
const DaoTool = require('../lib/daoTool');

exports.createOrder = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("insert into we_chat_pay_order set ?;", [DaoTool.generateModelObject(params, WeChatPayModel, 'create')], function (err, result) {
            printLog("=createOrder=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.updateOrder = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query('update we_chat_pay_order set ? where RechargeTradeNo=?;', [DaoTool.generateModelObject(params, WeChatPayModel, 'update'), params.RechargeTradeNo], function (err, result) {
            printLog("=updateOrder=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
exports.queryOrder = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query('select * from we_chat_pay_order where RechargeTradeNo=?;', [params.RechargeTradeNo], function (err, result) {
            printLog("=queryOrder=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
exports.getLastRechargeFee = function (conn,params) {
    return new Promise(function (resolve, reject) {
        conn.query('select * from we_chat_pay_order where openid=? order by RechargeId desc limit 0,1;', [params.openid], function (err, result) {
            printLog("=queryOrder=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};